import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-10-30
 * Time: 8:31
 */
public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++){
            a[i] = in.nextInt();
        }
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>(){
            public int compare(Integer o1,Integer o2){
                return o2 - o1;
            }
        });
        Queue<Integer> maxQue = new ArrayDeque<>();
        for (int i = 0; i < n; i++){
            if(i < k){
                maxHeap.offer(a[i]);
                minHeap.offer(a[i]);
            }else{
                System.out.print(minHeap.peek() + " ");
                maxQue.offer(maxHeap.peek());
                maxHeap.remove(a[i-k]);
                maxHeap.offer(a[i]);
                minHeap.remove(a[i-k]);
                minHeap.offer(a[i]);
            }
        }
        System.out.print(minHeap.peek() + " ");
        maxQue.offer(maxHeap.peek());
        System.out.println();
        while (!maxQue.isEmpty())
            System.out.print(maxQue.poll() + " ");
    }
}
